#if defined(ARCH_X86)
#include <immintrin.h>

// AVX2优化的矩阵乘法
ErrorCode matrix_multiply_avx2(const Matrix* a, const Matrix* b, Matrix** result) {
    if (a->cols != b->rows) return ERR_DIMENSION_MISMATCH;
    
    Matrix* out = matrix_create(a->rows, b->cols);
    if (!out) return ERR_MEMORY_ALLOC;
    
    const size_t block_size = 8; // AVX2处理8个双精度浮点数
    
    for (size_t i = 0; i < a->rows; i++) {
        for (size_t j = 0; j < b->cols; j += block_size) {
            __m256d sum = _mm256_setzero_pd();
            
            for (size_t k = 0; k < a->cols; k++) {
                __m256d a_val = _mm256_set1_pd(a->data[i * a->cols + k]);
                __m256d b_val = _mm256_loadu_pd(&b->data[k * b->cols + j]);
                sum = _mm256_fmadd_pd(a_val, b_val, sum);
            }
            
            _mm256_storeu_pd(&out->data[i * out->cols + j], sum);
        }
    }
    
    *result = out;
    return SUCCESS;
}
#endif